package com.gong.filter;

import com.gong.constants.UserConstants;
import com.gong.pojo.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: SysFilter
 * @Description: TODO
 * @Author: 。。。
 * @Data: 2021/10/209:42
 * @Version: 1.0
 **/
public class SysFilter implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		return;
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;

		User user = (User) request.getSession().getAttribute(UserConstants.USER_SESSION);

		if (user == null) {
			response.sendRedirect(request.getContextPath() + "/error.jsp");
		} else {
			filterChain.doFilter(servletRequest, servletResponse);
		}
	}

	@Override
	public void destroy() {
		return;
	}
}
